#!/usr/bin/python

from multiplylib import *


def new_fig():
    fig, ax = subplots()
    fig.set_facecolor("white")
    fig.set_dpi(72)
    fig.set_size_inches(16.0, 5.0)
    ax.set_xlim(0,350)
    ax.set_ylim(0,140)
    ax.set_frame_on(False)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    return (fig, ax)



# init the dots grid params


# dots_16x16 = gen_pp(mcand_width=16, mplier_width=16)   
# fig1, ax1 = new_fig()
# display_dots(ax1, dots_16x16, pos_base=(0,1))

# savefig("fig.png", dpi=72)








## ========== check the coverage ==========
m16_dots_cnt = 0
m8_dots_cnt = 0
m16_dots_only_cnt = 0
m8_dots_only_cnt = 0
for col in range(32):
   for row in range(9):
      if dots_8x8_up[row][col][0] != 0:
          m8_dots_cnt += 1
      if dots_16x16_up[row][col][0] != 0:
          m16_dots_cnt += 1
      if (dots_8x8_up[row][col][0] != 0) and (dots_16x16_up[row][col][0] == 0):
          print "dots[%d][%d] for 8x8 mode is not covered by 16x16 mode" % (row, col)
          m8_dots_only_cnt += 1
      if (dots_16x16_up[row][col][0] != 0) and (dots_8x8_up[row][col][0] == 0):
          print "dots[%d][%d] for 16x16 mode is not covered by 8x8 mode" % (row, col)
          m16_dots_only_cnt += 1

# if flag == True:
#     print "all dots in 8x8 mode is covered by 16x16 mode"

print "dots in 16x16 mode is : %d" % m16_dots_cnt
print "dots in 8x8 mode is : %d" % m8_dots_cnt
print "%d dots in 8x8 mode is not covered by 16x16 mode( 8x8 mode only)" % m8_dots_only_cnt
print "%d dots in 16x16 mode is not covered by 8x8 mode( 16x16 mode only)" % m16_dots_only_cnt

# # print the col expression
# print_col_formula(dots_8_up)
# print_col_formula(dots_16_up)

cols_8x8 = get_col_items(dots_8x8_up)
cols_16x16 = get_col_items(dots_16x16_up)
for items in cols_8x8:
    items.sort(reverse=True)
for items in cols_16x16:
    items.sort(reverse=True)

cols_mix = []
for col in range(32):
   col_common = []
   col_16x16_only = []
   col_8x8_only = []
   for i in cols_8x8[col]:
      if i in cols_16x16[col]:
         cols_16x16[col].remove(i)
         col_common.append(i)
      else:
         col_8x8_only.append(i)
   col_16x16_only = cols_16x16[col]
   # print "col_8x8[%d] = %s;" % (col, ' + '.join(col_common + col_8x8_only))
   # print "col_16x16[%d] = %s;" % (col, ' + '.join(col_common + col_16x16_only))

   # construct the new column for both 16x16/8x8 mode
   alternate_len = min(len(col_8x8_only), len(col_16x16_only))
   for i in range(alternate_len):
       d8 = col_8x8_only.pop(0)
       d16 = col_16x16_only.pop(0)
       col_common.append("(mode8 ? %s : %s)" % (d8, d16))
   for d8 in col_8x8_only:
       col_common.append("(mode8 & %s)" % d8)
   for d16 in col_16x16_only:
       col_common.append("(~mode8 & %s)" % d16)

   cols_mix.append(col_common)

dots_mix = gen_empty_dots_array(10, 32)
for col, items in enumerate(cols_mix):
   val_list = [(i,False) for i in items]
   set_dots_col(dots_mix, col, val_list)

print "==================== row formula ===================="
print_row_formula(dots_mix)
print "====================================================="

fig7, ax7 = new_fig()
display_dots(ax7, dots_mix, pos_base=(0,1), style="text")

# for col,items in enumerate(cols_mix):
#    print "col_mix[%d] = %s;" % (col, ' + '.join(items))
# print_col_formula(dots_mix)     # 

# FIXME: print the only and common dots ???



# for col in range(16,31+1,1):
#    for row in range(rows_num-1):
#       dots_8[row+1][col] = dots_8_H[row][col-16]



show()
exit()






for i in range(3):
    fig, ax = plt.subplots()
    ax.set_xlim(0,300)                       # must set the xlim/ylim manually
    ax.set_ylim(0,300)
    dots_16 = gen_pp(mcand_width=16, mplier_width=16)
    # print_col_formula(dots_16)
    display_dots(dots_16, pos_base=(0,1), style="dot", color="red")
show()

# dots_8_L = gen_pp(mcand_width=8, mplier_width=8, prefix="L_")
# dots_8_H = gen_pp(mcand_width=8, mplier_width=8, prefix="H_", pp0_height_reduction=False) # not reduce the height, for counteract the "1"

# display_dots(dots_8_L, pos_base=gen_next_pos())
# display_dots(dots_8_H, pos_base=gen_next_pos())
